package com.infosystem.demo.helpers;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class IdNumberHelper {
    public IdNumberHelper(){

    }

    // validate ID number
    public boolean validateID(String rawId) throws ParseException {
        String id = rawId.toUpperCase();

        // generate regex pattern
        String idRegex = "^[1-6]\\d{5}(18|19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9X]";

        if (!id.matches(idRegex)) {
            return false;
        }

        //validate province code
        int[] d={11,12,13,14,15,
                21,22,23,31,32,33,34,35,36,37,
                41,42,43,44,45,46,50,51,52,53,53,
                61,62,63,64,65,83,81,82};
        int prov=Integer.parseInt(id.substring(0, 2));
        boolean provValid = false;
        for(int i=0;i<d.length;i++){
            if(d[i]==prov) {
                provValid = true;
                break;
            }
        }
        if(!provValid) {
            return false;
        }

        //birthdate <= current date
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        int bDate = Integer.parseInt(id.substring(6, 14));
        int curDate = Integer.parseInt(dateFormat.format(new Date()));
        if(bDate > curDate) {
            return false;
        }

        /** Copyright: https://blog.csdn.net/qq_43290318/article/details/101078763 */
        int year=Integer.parseInt(id.substring(6, 10));
        int leap=((year%4==0 && year%100!=0) || year%400==0)?1:0;
        final int[] month={0,31,28+leap,31,30,31,30,31,31,30,31,30,31};
        int mon=Integer.parseInt(id.substring(10, 12));
        int day=Integer.parseInt(id.substring(12, 14));
        if(day>month[mon])
        {
            //System.out.println(day+" "+month[mon]+"\n");
            //System.out.println("---");
            return false;
        }

        //validate last char
        if (id.charAt(17) != getLastChar(id)) {
            return false;
        }

        return true;
    }

    /** Copyright: https://blog.csdn.net/qq_43290318/article/details/101078763 */
    public char getLastChar(String id) //由于这个功能比较独立，就分离出来
    {
        final int[] w={0,7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
        final char[] ch={'1','0','X','9','8','7','6','5','4','3','2'}; //这就是为什么一开始将末尾可能存在的x转成X的原因
        int res=0;
        for(int i=0;i<17;i++)
        {
            int t=id.charAt(i)-'0';
            res+=(t*w[i+1]);
        }
        return ch[res%11];
    }

    /** Copyright: https://blog.csdn.net/qq_43290318/article/details/101078763 */
    public int getAge(String id) {
        int yearBirth=Integer.parseInt(id.substring(6, 10));
        int monBirth=Integer.parseInt(id.substring(10, 12));
        int dayBirth=Integer.parseInt(id.substring(12, 14));

        Calendar cur=Calendar.getInstance();
        int curYear=cur.get(Calendar.YEAR);
        int curMon=cur.get(Calendar.MONTH) + 1; //月份从0开始，所以不要忘了+1
        int dayCur =cur.get(Calendar.DATE);


        int age=curYear-yearBirth;
        if(curMon<monBirth || (curMon==monBirth && dayCur<dayBirth)) {
            age--;
        }
        return age;
    }
}
